package whf.framework.workflow.assign.parser;

import java.util.Collection;
import java.util.Set;

import org.jdom.Element;

import whf.framework.exception.FindException;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.workflow.assign.AssignmentService;


/**
 * @author king
 *
 */
public class DeptAssignmentParser extends DefaultAssignmentParser implements DeptableAssignmentParser, AssignmentParser{
	private static Log log = LogFactory.getLog(DeptAssignmentParser.class);
	
	private Collection<String> depts;
	
	public Collection<String> getDeptCodes() {
		return depts;
	}

	public void setDeptCodes(Collection<String> depts) {
		this.depts = depts;
	}
	
	protected void parseSourceElement(Element source) {
		String code = super.getAttribute("code");
		String name = super.getAttribute("name");
		try{
			this.setDeptCodes(AssignmentService.getAssignmentService().findDepts(code, name));
		}catch(Exception e) {
			log.error(e);
		}
	}
	
	public Collection<String> getUsers() throws FindException {
		Set<AssignmentParser> childrenParsers = this.getChildrenParsers();
		if(childrenParsers == null || childrenParsers.isEmpty()) {
			return null;
		} else {
			return AssignmentService.getAssignmentService().findUsersByDepts(depts, super.getText());
		}
	}

}
